package cn.doit.day03

import scala.util.Sorting
import cn.doit.day03.AnyThing._

object _08_比较器和可比较的 {
  def main(args: Array[String]): Unit = {//
    val orders: Array[ScalaOrder] = Array(
      ScalaOrder("o001", "sn001", "g001", "c001", 2, 9.9),
      ScalaOrder("o002", "sn002", "g002", "c002", 1, 1400),
      ScalaOrder("o003", "sn003", "g003", "c003", 3, 189),
      ScalaOrder("o005", "sn005", "g005", "c005", 1, 2008),
      ScalaOrder("o004", "sn004", "g004", "c004", 54, 800),
      ScalaOrder("o008", "sn008", "g008", "c008", 3, 356),
      ScalaOrder("o007", "sn007", "g007", "c007", 8, 19.9),
      ScalaOrder("o006", "sn006", "g006", "c006", 1, 39.9)
    )

/*    implicit val value: Ordering[ScalaOrder] = new Ordering[ScalaOrder] {
      override def compare(x: ScalaOrder, y: ScalaOrder) = -(x.goods_number * x.price).compare(y.goods_number * y.price)
    }*/
    //进行一个排序 按照订单的金额降序排列
    Sorting.quickSort(orders)/*(new Ordering[ScalaOrder] {
      override def compare(x: ScalaOrder, y: ScalaOrder) = (x.goods_number*x.price).compare(y.goods_number*y.price)
    })*/

    for (elem <- orders) {
      println(elem)
    }

    //隐式转换

  }

}

case class ScalaOrder(  order_id: String
                      , order_sn: String
                      , goods_id: String
                      , category_id: String
                      , goods_number: Int
                      , price: Double) extends Ordered[ScalaOrder] {
  override def compare(that: ScalaOrder): Int = -(this.price*this.goods_number) .compare(that.goods_number*that.price)
}
